package com.ljh.myrpc.server;

import com.ljh.myrpc.server.HttpServer;
import io.vertx.core.Vertx;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class VertxHttpServer implements HttpServer {

    @Override
    public void doStart(int port) {
        // 创建 Vert.x 实例
        Vertx vertx = Vertx.vertx();

        //  创建 HTTP 服务器
        io.vertx.core.http.HttpServer server = vertx.createHttpServer();

//        server.requestHandler(request -> {
//            // 处理HTTP请求
//            log.debug("Received request: {} + {}", request.method(), request.uri());
//            // 发送HTTP响应
//            request.response()
//                    .putHeader("content-type", "text/plain")
//                    .end("Hello from Vert.x HTTP Server!");
//        });
        server.requestHandler(new HttpServerHandler());

        // 启动HTTP服务器并监听指定端口
        server.listen(port, result -> {
            if (result.succeeded()) {
                log.info("HTTP server started on port {}", port);
            } else {
                log.error("Failed to start HTTP server", result.cause());
            }
        });
    }
}
